<?php
class Contact extends MY_Controller
{
    function __construct() {
        parent::__construct();
        $this->load->model('mcontact');
        $this->load->helper('mail_helper');
    }
    function index() {
		$data['act'] = 4;
        $data['title'] = 'Contact us';
        $data['curent'] = 'contact';
        $data['template'] = 'contact/layout';
        $this->load->view('contact/layout',$data);
    }
    public function ajax(){
		   $name  = $this->fillter($this->input->post("name"));
		   $phone = $this->fillter($this->input->post("phone"));
		   $email = $this->fillter($this->input->post("email"));
		   $title = $this->fillter($this->input->post("title"));
		   $info  = $this->fillter($this->input->post("content"));
		   $starok = "";
		   if($name != NULL && $email != NULL && $info != NULL && $title != NULL){
				$null = "/^[a-zA-Z]{1}[a-zA-Z0-9._]{3,25}\@[a-zA-Z0-9]{3,}\.[a-zA-Z.]{2,8}$/";
				if(preg_match($null,$email)){
					$starok = TRUE;
				}else{
					echo "Email is invalid <br />";
				}
				if($starok == TRUE){
					$data = array(
							"name" => $name,
							"email" => $email,
							"phone" => $phone,
							"subject" => $title,
							"information" => $info,
							"time" => date("d/m/Y - h:i:s")
						);
					$this->sendmail($data);
					$this->model_home->insertcontact($data);
					echo 3;
				}
			}else{
				echo 1;
			}
	   }
	   public function sendmail($data){
		   $mesnger = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                    <html>
                    <head>
                    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                    </head>
                    <body>
                    <h2 style="font-size: 16px;">Contact From Hanoi Sapa Train</h2>
                    <br/><br/>
                    <p>CONTACT INFORMATION</p>

            ';
			$mesnger .= "Fullname : ".$data['name']." <br />Email : ".$data['email']." <br />Phone : ".$data['phone']." <br />Subject : ".$data['subject']." <br />Question : ".$data['information']."";
			$mesnger .= '</body></html> ';
			send_mail_helper('haanhdon@gmail.com', 'CONTACT FROM HANOISAPA-TRAIN', htmlspecialchars_decode($mesnger));
	   }
}
?>